<template>
	<!-- 添加出货单 -->
	<div class="page" v-loading="loading">
		<el-breadcrumb class="breadcrumb" separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>添加集采单</el-breadcrumb-item>
		</el-breadcrumb>
		<el-form class="content" ref="form" :model="form" status-icon :rules="rules" label-width="120px">
			<el-form-item label="类型" prop="stockUseType">
				<span>集采单</span>
			</el-form-item>
			<el-form-item label="金额" prop="amount">
				<el-input size="small" type="number" v-model="form.amount"></el-input>
			</el-form-item>
			<el-form-item label="发票编号" prop="invoiceNumber">
				<el-input size="small" type="number" v-model="form.invoiceNumber"></el-input>
			</el-form-item>
			<el-form-item label="收付款凭证号" prop="paymentNumber">
				<el-input size="small" type="number" v-model="form.paymentNumber"></el-input>
			</el-form-item>
			<el-form-item label="数量" prop="qty">
				<el-input size="small" type="number" v-model="form.qty"></el-input>
			</el-form-item>

			<select-one-supplier v-on:sendGoodsData="getSupplierData"></select-one-supplier>
			<select-more-goods v-on:sendSomeGoodsData="getMoreGoodsData" ref="moreGoods"></select-more-goods>

			<el-form-item label="单据编号" prop="receiptNumber">
				<el-input size="small" type="number" v-model="form.receiptNumber"></el-input>
			</el-form-item>
			<el-form-item label="单位" prop="unit">
				<el-input size="small" v-model="form.unit"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input size="small" v-model="form.remark"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button size="small" type="primary" @click="submitForm('form')">立即创建</el-button>
				<el-button size="small" @click="utils.back">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
import axios from 'axios';
import selectOneSupplier from '../../common/selectOneSupplier.vue';
import selectMoreGoods from '../../common/selectMoreGoods.vue';
export default {
	name: "add",
	data() {
		return {
			form:{
				"amount": 0,				//总金额 单位分
				"invoiceNumber": "",		//发票编号
				"paymentNumber": "",		//收付款凭证号
				"qty": 0,					//总数量
				"receiptNumber": "",		//单据编号
				"stockUseType": "INSINGLE",		//单据类型
				"supplierContact": "",		//供应商联系人
				"supplierId": '',			//供应商ID
				"supplierMobile": "",		//手机号
				"supplierName": "",			//供应商
				"unit": "",					//单位 （瓶）
				"remark": "",				//备注
				"stockUseItems": [
				]
			},
			rules: {
				amount: [{required: true,message: '金额不能为空',trigger: 'blur'}],
				invoiceNumber: [{required: true,message: '发票编号不能为空',trigger: 'blur'}],
				paymentNumber: [{required: true,message: '收付款凭证号不能为空',trigger: 'blur'}],
				qty: [{required: true,message: '数量不能为空',trigger: 'blur'}],
				receiptNumber: [{required: true,message: '单据编号不能为空',trigger: 'blur'}],
				stockUseType: [{required: true,message: '类型不能为空',trigger: 'blur'}],
				unit: [{required: true,message: '单位不能为空',trigger: 'blur'}],
				remark: [{required: true,message: '备注不能为空',trigger: 'blur'}],
			},
			loading: false,
			city: ''
		}
	},
	components: {
		selectOneSupplier,
		selectMoreGoods
	},
	created() { },
	mounted(){
	},
	methods: {
		// 获取子组件（选择商品）数据
		getMoreGoodsData(data){
			console.log(data,this.form.stockUseItems)
			for(var i in data){
				data[i].goodsId = data[i].id;
				this.form.stockUseItems.push(data[i]);
			}
		},
		// 获取子组件（选择供应商）数据
		getSupplierData(dataID,data){
			console.log(dataID,data[0])
			this.form.supplierContact = dataID;
			this.form.supplierId = dataID;
			this.form.supplierMobile = data[0].supplierChannelNo;		//供应商手机
			this.form.supplierName = data[0].supplierName;

		},
		// 添加进货单
		submitForm(formName) {
			this.loading = true;
			this.$refs[formName].validate((valid) => {
				if (valid) {
					this.$refs.moreGoods.sendData();
					this.$post("stockUseAdd", this.form).then((res) => {
						this.loading = false;
						if (res.code == 200) {
							this.$msg.success("添加成功～");
							this.$router.back();
						} else {
							this.$msg.warning(res.message);
						}
					})
				} else {
					this.$msg.error("网络或服务器错误");
					this.loading = false;
				}
				this.form.stockUseItems = [];
			});
		},
	}
};
</script>
<style lang="less" scoped>
.page {
  .content {
    width: 1000px;
    margin: 0 auto;
    .el-input {
      width: 600px;
    }
    .add-data {
      .el-input {
        width: 80%;
      }
    }
  }
}
</style>